package com.kuang.demo.service.wx;

import com.kuang.demo.bean.wx.WXStorageUpload;
import com.kuang.demo.mapper.StorageMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class WXStorageServiceImpl implements WXStorageService {
    @Autowired
    StorageMapper storageMapper;
    @Value("${25mall.pic.upload}")
    String picUploadPath;

    @SneakyThrows
    @Override
    public WXStorageUpload upload(String url, MultipartFile file) {
        UUID uuid = UUID.randomUUID();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = dateFormat.format(new Date());
        int size = (int) file.getSize();
        String contentType = file.getContentType();
        String name = file.getOriginalFilename();
        WXStorageUpload dataBean = new WXStorageUpload();
        dataBean.setKey("WX" + uuid + ".jpg");
        dataBean.setName(name);
        dataBean.setSize(size);
        dataBean.setAddTime(time);
        dataBean.setUpdateTime(time);
        dataBean.setType(contentType);
        File file1 = new File(picUploadPath, uuid + ".jpg");
        System.out.println(file1);
        dataBean.setUrl(url + uuid + ".jpg");
        storageMapper.insertWXFileUpPic(dataBean.getName(), dataBean.getType(), dataBean.getSize(), dataBean.getUrl(), dataBean.getAddTime(), dataBean.getUpdateTime(), dataBean.getKey());
        file.transferTo(file1);
        return dataBean;
    }
}
